<?php
namespace Learn\Base;


abstract class Behavior
{
    public $owner;
    // 绑定行为(方法)到对象
    // 例如：$user->attachBehavior('mailer', ['class' => 'Mailer']);
    public function attach($to)
    {
        $this->owner = $to;
        $methods = get_class_methods($this);
        foreach ($methods as $methodName) {
            if (strpos($methodName, 'on') === 0 && strlen($methodName) > 2) {
                $method = lcfirst(substr($methodName, 2));
                $func = [$this, $methodName];
                $this->owner->on($method, $func);
            }
        }
    }
}